//
//  MainNaviController.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/18.
//

import UIKit

class MainNaviController: UINavigationController {

    // MARK: - 自定义属性
    var isStatusBarHidden: Bool = false
    var cusPreferredStatusBarStyle: UIStatusBarStyle = .default
    
    // MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavigationBar()
    }
    
    // MARK: - 状态栏样式
    override var prefersStatusBarHidden: Bool {
        return isStatusBarHidden
    }
    
    override var childForStatusBarHidden: UIViewController? {
        return nil
    }
    
    override var childForStatusBarStyle: UIViewController? {
        return nil
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .fade
    }
    
    // MARK: - 导航栏设置
    private func setupNavigationBar() {
        // 创建导航栏外观配置
        let appearance = UINavigationBarAppearance()
        
        // 设置背景样式
        appearance.configureWithOpaqueBackground()
        appearance.shadowColor = nil //去除分割线
        appearance.backgroundColor = AppTheme.Colors.naviBgColor //导航栏背景色
        
        // 设置标题样式
        appearance.titleTextAttributes = [
            .foregroundColor: AppTheme.Colors.naviTitleColor,
            .font: UIFont.systemFont(ofSize: 18, weight: .semibold)
        ]
        
        // 设置返回按钮样式
        appearance.backButtonAppearance.normal.titleTextAttributes = [
            .foregroundColor: AppTheme.Colors.naviBackButtonColor
        ]
        
        // 应用外观配置
        navigationBar.standardAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
        navigationBar.compactAppearance = appearance
        
        // 其他导航栏设置
        navigationBar.tintColor = AppTheme.Colors.navigationBarTintColor // 按钮颜色
        navigationBar.isTranslucent = false
    }
    
    // MARK: - 视图控制器管理
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 自定义push行为
        if children.count > 0 {
            // 非根视图控制器添加返回按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(
                image: UIImage(systemName: "chevron.left"),
                style: .plain,
                target: self,
                action: #selector(popViewControllerAnimated)
            )
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    // MARK: - 交互方法
    @objc private func popViewControllerAnimated() {
        popViewController(animated: true)
    }
    
    // MARK: - 便捷方法
    func setStatusBarHidden(_ hidden: Bool, animated: Bool = false) {
        if animated {
            UIView.animate(withDuration: 0.3) {
                self.isStatusBarHidden = hidden
                self.setNeedsStatusBarAppearanceUpdate()
            }
        } else {
            isStatusBarHidden = hidden
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    func setStatusBarStyle(_ style: UIStatusBarStyle, animated: Bool = false) {
        if animated {
            UIView.animate(withDuration: 0.3) {
                self.cusPreferredStatusBarStyle = style
                self.setNeedsStatusBarAppearanceUpdate()
            }
        } else {
            self.cusPreferredStatusBarStyle = style
            setNeedsStatusBarAppearanceUpdate()
        }
    }

}
